<?php

namespace App\Services\Gallery\Services;

use App\Exceptions\ServiceException;
use App\Services\Gallery\Interfaces\SpaceSchemeContentServiceInterface;
use App\Services\Gallery\Model\SpaceSchemeContent;
use Illuminate\Support\Facades\Auth;

class SpaceSchemeContentService implements SpaceSchemeContentServiceInterface
{

    public function set($request, $id = 0)
    {
        if ($id) {
            $data = SpaceSchemeContent::findOrFail($id);
        } else {
            $data = new SpaceSchemeContent();
        }
        $data->user()->associate(Auth::id());
        $data->fill($request)->save();

        return $data->toArray();
    }

    public function get($id)
    {
        return SpaceSchemeContent::query()->where('id', $id)->firstOrFail()->toArray();
    }

    public function destroy($id)
    {
        $data = SpaceSchemeContent::findOrFail($id);
        $data->delete();
    }

    public function list()
    {
        $data = SpaceSchemeContent::query()->show()->orderBy('sort','asc')->get();

        return $data->toArray();
    }
}
